home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1216 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  134 lines

  1. Path: news.cis.nctu.edu.tw!usenet
  2. From: terryt@mcs.com (Terry Trippany)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie - inheritance ques!
  5. Date: 9 Jan 1996 21:32:46 GMT
  6. Organization: STR/Baxter Labs
  7. Message-ID: <4cumtv$sso@news.cis.nctu.edu.tw>
  8. References: <madhavi.820163040@class1.iastate.edu> <4bvocb$5k1@werple.net.au> <30E41252.35D5@ix.netcom.com>
  9. NNTP-Posting-Host: @159.198.73.111
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <30E41252.35D5@ix.netcom.com>, susaned@ix.netcom.com 
  15. says...
  16. >
  17. >David White wrote:
  18. >> 
  19. >> madhavi@iastate.edu (Madhavi S Nuttaki) writes:
  20. >> 
  21. >> >Hi,
  22. >> 
  23. >> >       I am trying to figure out what is happening in the 
  24. following
  25. >> >scenario.
  26. >> 
  27. >> >       class1                       class2
  28. >> >                                        ^
  29. >> >                                       |
  30. >> >                                    class3
  31. >> 
  32. >> >class3 inherits from class2.
  33. >> >class1 contains a class2 object.
  34. >> 
  35. >> >I overloaded the '<' operator (defined int class3, virtual in
  36. >> >class2).
  37. >> 
  38. >> >Lets say class1  has a data element -  class2 *tmp;
  39. >> 
  40. >> >       In one of class1's methods I (which receives a class2 
  41. *tmp1)
  42. >> >do this:
  43. >> 
  44. >> >               it(tmp < tmp1) - now this should call the 
  45. overloaded
  46. >> >function defined in class3 (according to my understanding).
  47. >> 
  48. >> >       But this is not happening. I am having to explicitly call 
  49. hte
  50. >> >overloaded func.
  51. >> 
  52. >> >Can anyone help me figure this out.
  53. >> 
  54. >> The code above is comparing two pointers, not two objects.
  55. >> Change it to: if(*tmp < *tmp1).
  56. >> 
  57. >> David White
  58. >> davidw@werple.mira.net.au
  59. >
  60. Hello all,
  61.  
  62.   One of the first things that should be done to make things simpler 
  63. is to eliminate the pointers and concentrate on the virtual 
  64. inheritence.  Simply put, classes of type 2 will use its' definition 
  65. of the operator and classes of type 3 will use the overloaded one.  
  66. Virtuals get tricky when you aren't careful using multiple 
  67. inheritance.  You have to watch the type of classes on the left hand 
  68. side of this operator.
  69.  
  70. eg: take the following simple example:
  71.  
  72. #include <fstream.h>
  73.  
  74. class y 
  75. {
  76. public :
  77.   virtual int operator <(y) { cout << "Y Virt\n"; return 1;};
  78. };
  79.  
  80. class z : public y
  81. {
  82. public:
  83.     int operator <(y) { cout << "Z overload\n"; return 2;};
  84. };
  85.  
  86. class w : public y
  87. {
  88.  
  89. };
  90.  
  91. class x
  92. {
  93.     y myY;
  94. public :
  95.     void testVirtual(y yIn){ myY < yIn;}; // 2 of type Y
  96. };
  97.  
  98. int main()
  99. {
  100.     y myYclass;
  101.     x myXclass;
  102.     z myZclass;
  103.     w usesVirt;
  104.  
  105.     myXclass.testVirtual(myYclass); // will use virtual Y
  106.     myZclass < myYclass;            // will use Z overload 
  107.         myZclass < usesVirt;        // will use z overload
  108.     usesVirt < myZclass;            // will use inherited from y
  109.  
  110.     return 0;
  111. }
  112.  
  113.  
  114.  
  115. The following is the output:
  116. Y Virt
  117. Z overload
  118. Z overload
  119. Y Virt
  120.  
  121. Sometimes it is a good idea to make your classes streamable for 
  122. debugging purposes.  Later,
  123.  
  124. Terry Trippany
  125. Strategic Technology Resources
  126. Chicago, IL 
  127.  
  128. @#?#%% Compiler
  129.  
  130.  
  131.  
  132.  
  133.  
  134.